package com.kgc.web;

import com.kgc.dao.UserDao;
import com.kgc.dao.UserHomeDao;
import com.kgc.entity.*;
import com.kgc.untils.CodecUtils;
import com.kgc.untils.PageUtils;
import com.kgc.vo.R;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@WebServlet(name = "UserServlet", value = "/UserServlet")

public class UserServlet extends HttpServlet {
    private static final String KEY_PHONE = "sms:phone:"; // Session,key前缀

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserHomeDao userHomeDao = new UserHomeDao();
        UserDao userDao = new UserDao();
        HttpSession session = request.getSession();
        PrintWriter writer = response.getWriter();
        String opt = request.getParameter("opt");
        PageUtils pageUtils = new PageUtils();
        if ("total".equals(opt)) {
            User user = (User) session.getAttribute("user");
            int totalTouSu = userHomeDao.totalTouSu();
            session.setAttribute("totalTouSu", totalTouSu);
            int totalMyTouSu = userHomeDao.totalMyTouSu(user.getId());
            session.setAttribute("totalMyTouSu", totalMyTouSu);
            int totalRepair = userHomeDao.totalRepair();
            session.setAttribute("totalRepair", totalRepair);
            int totalMyRepair = userHomeDao.totalMyRepair(user.getId());
            session.setAttribute("totalMyRepair", totalMyRepair);
            int totalPayment = userHomeDao.totalPayment();
            session.setAttribute("totalPayment", totalPayment);
            int totalMyPayment = userHomeDao.totalMyPayment(user.getId());
            session.setAttribute("totalMyPayment", totalMyPayment);
            String phone = user.getPhone();
            session.setAttribute("phone", phone);
            int i = user.getSex();
            String sex;
            if (i == 0) {
                sex = "女";
            } else {
                sex = "男";
            }
            Integer id = user.getId();
            session.setAttribute("sex", sex);
            List<UserRoom> userRooms = userDao.selectRoomByUserId(id);
            String myUserRoom;
            if (userRooms.size() > 0) {
                StringBuffer sb = new StringBuffer();
                for (UserRoom userRoom : userRooms) {
                    sb.append(userRoom.getRoomName()).append(",");
                }
                myUserRoom = sb.deleteCharAt(sb.length() - 1).toString();
            } else {
                myUserRoom = "暂无";
            }
            session.setAttribute("myUserRoom", myUserRoom);
            StringBuffer sb1 = new StringBuffer();
            List<UserCar> userCars = userDao.selectCarByUserId(id);
            String myUserCar;
            if (userCars.size() > 0) {
                for (UserCar userCar : userCars) {
                    sb1.append(userCar.getCarname()).append(",");
                }
                myUserCar = sb1.deleteCharAt(sb1.length() - 1).toString();
            } else {
                myUserCar = "暂无";
            }
            session.setAttribute("myUserCar", myUserCar);
            request.getRequestDispatcher("/page/user/console.jsp").forward(request, response);
        } else if ("addTouSu".equals(opt)) {
            Date date = new Date();
            long time = date.getTime();
            Timestamp timestamp = new Timestamp(time);
            String content = request.getParameter("content");
            User user = (User) session.getAttribute("user");
            int i = userHomeDao.addTouSu(new Tousu(content, user.getId(), timestamp));
            if (i > 0) {
                writer.write(R.success("添加成功!").toJSON());
            } else {
                writer.write(R.error("添加失败!").toJSON());
            }
        } else if ("addWeiXiu".equals(opt)) {
            Date date = new Date();
            long time = date.getTime();
            Timestamp timestamp = new Timestamp(time);
            String content = request.getParameter("content");
            User user = (User) session.getAttribute("user");
            int i = userHomeDao.addRepair(new Repair(content, user.getId(), timestamp));
            if (i > 0) {
                writer.write(R.success("添加成功!").toJSON());
            } else {
                writer.write(R.error("添加失败!").toJSON());
            }
        } else if ("updatePwd".equals(opt)) {
            String oldpwd = request.getParameter("oldpwd");
            String newpwd1 = request.getParameter("newpwd1");
            String newpwd2 = request.getParameter("newpwd2");
            User user = (User) session.getAttribute("user");
            Integer id = user.getId();
            String pwd = userDao.selectPwd(user.getId());
            String salt = user.getSalt();
            String newpwd = CodecUtils.md5Hex(newpwd2, salt);
            if (CodecUtils.match(pwd, oldpwd, salt)) {
                if (newpwd1.equals(newpwd2)) {
                    int i = userDao.updatePwd(newpwd, id);
                    if (i > 0) {
                        writer.write(R.success("密码修改成功!").toJSON());
                    } else {
                        writer.write(R.success("密码修改失败!").toJSON());
                    }
                } else {
                    writer.write(R.error("两次密码输入不一致").toJSON());
                }
            } else {
                writer.write(R.error("原始密码输入错误!").toJSON());
            }
        } else if ("gongGaoList".equals(opt)) {
            String page = request.getParameter("page");
            String limit = request.getParameter("limit");
            int totalGongGao = userDao.totalGongGao();
            if (page != null) {
                pageUtils.setCurrPageNo(Integer.parseInt(page));
            }
            if (limit != null) {
                pageUtils.setPageSize(Integer.parseInt(limit));
            }
            pageUtils.init(totalGongGao);
            List<Gonggao> GongGaoList = userDao.selectGongGao(pageUtils);
            writer.write(R.success("数据加载成功", totalGongGao, GongGaoList).toJSON());
        } else if ("TouSuList".equals(opt)) {
            String page = request.getParameter("page");
            String limit = request.getParameter("limit");
            User user = (User) session.getAttribute("user");
            Integer id = user.getId();
            int totalTouSu = userDao.totalTouSu(id);
            if (page != null) {
                pageUtils.setCurrPageNo(Integer.parseInt(page));
            }
            if (limit != null) {
                pageUtils.setPageSize(Integer.parseInt(limit));
            }
            List<Tousu> myTouSuList = userDao.selectMYTouSu(pageUtils, id);
            pageUtils.init(totalTouSu);
            writer.write(R.success("数据加载成功", totalTouSu, myTouSuList).toJSON());
        } else if ("WeiXiuList".equals(opt)) {
            String page = request.getParameter("page");
            String limit = request.getParameter("limit");
            User user = (User) session.getAttribute("user");
            Integer id = user.getId();
            int totalWeiXiu = userDao.totalWeiXiu(id);
            if (page != null) {
                pageUtils.setCurrPageNo(Integer.parseInt(page));
            }
            if (limit != null) {
                pageUtils.setPageSize(Integer.parseInt(limit));
            }
            List<Tousu> myWeiXiuList = userDao.selectMYWeiXiu(pageUtils, id);
            pageUtils.init(totalWeiXiu);
            writer.write(R.success("数据加载成功", totalWeiXiu, myWeiXiuList).toJSON());
        } else if ("zhangdan".equals(opt)) {
            String page = request.getParameter("page");
            String limit = request.getParameter("limit");
            User user = (User) session.getAttribute("user");
            Integer id = user.getId();
            int totalPayment = userDao.totalPayment(id);
            if (page != null) {
                pageUtils.setCurrPageNo(Integer.parseInt(page));
            }
            if (limit != null) {
                pageUtils.setPageSize(Integer.parseInt(limit));
            }
            List<UserPayment> selectPaymentById = userDao.selectPaymentById(pageUtils, id);
            pageUtils.init(totalPayment);
            writer.write(R.success("数据加载成功", totalPayment, selectPaymentById).toJSON());
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
